# 顾客登录账号的状态管理和展示
from rest_framework.views import APIView
from rest_framework.response import Response
from rest_framework.decorators import api_view
from django.db.models import Q
from rest_framework import status
from PetNetadmin.serializers import CustomLoginMessageSerializer
from utils.pagination import PetMessagePagination
from NetShop.models import CustomLogin
from admincharge.utils.jwt import MyJWTAuthentication


class GetAllCustomLoginMessage(APIView):
    authentication_classes = [MyJWTAuthentication]
    def get(self,request):
        print(request.GET.get)
        username = request.GET.get('username')
        mobile = request.GET.get('mobile')
        status = request.GET.get('status')
        # 构建查询条件
        conditions = Q()
        if username:
            conditions &= Q(username=username)
        if mobile:
            conditions &= Q(mobile=mobile)
        if status:
            conditions &= Q(status=status)
        # 应用查询条件或返回所有数据
        if conditions:
            queryset = CustomLogin.objects.filter(conditions)
        else:
            queryset = CustomLogin.objects.all()
        # 分页处理
        paginator = PetMessagePagination()
        result_page = paginator.paginate_queryset(queryset, request)
        serializer = CustomLoginMessageSerializer(instance=result_page, many=True)
        result = {
            'data': serializer.data,
            'code': 2000,
            'msg': '获取成功'
        }
        return paginator.get_paginated_response(result)


@api_view(['POST'])
def changeStatus(request):
    username = request.GET.get('username')
    status = request.GET.get('status')
    print(username, status)
    custom_login = CustomLogin.objects.get(username=username)
    custom_login.status = status
    custom_login.save()
    result = {
        'code': 2000,
        'msg': '修改成功'
    }
    return Response(result)